package com.mak001.lite.multiSkill.subs;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Skills;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.util.Timer;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Tile;

public class CookerLite extends subScript {
	int tinder = 590;
	int[] fire = { 2732, 12269 };
	int[] cooked = { 13433, 315, 325, 347, 319, 3144, 355, 333, 339, 351, 3381,
			329, 361, 5003, 10136, 379, 365, 373, 7946, 385, 397, 391, 15266,
			15272, 1861, 2140, 2142, 9988, 9980 };

	Time time = new Time();
	Timer animTimer = new Timer(3000);
	int cook = 0;

	int raw;
	int logs;
	String fishName;
	private int numFish;
	int ge = 0;

	Tile fTile;
	Tile bTile;

	private enum state {
		BANK, COOK, LIGHT, GETLOGS
	}

	private state getState() {
		if (Inventory.contains(raw)) {
			GameObject Fire = Objects.getNearest(fire);
			if (Fire != null && Fire.getLocation().equals(fTile)) {
				return state.COOK;
			} else {
				if (Inventory.contains(logs)
						&& Players.getLocal().getAnimation() == -1 && animTimer.isRunning()) {
					return state.LIGHT;
				} else {
					return state.GETLOGS;
				}
			}
		} else {
			if (Inventory.contains(logs)) {
				return state.BANK;
			} else {
				return state.GETLOGS;
			}
		}
	}

	@Override
	public void execute() {
		if (Methods.GrandExchange.contains(Players.getLocal().getLocation())) {
			if (ge == 0 || ge == -1) {
				ge = Random.nextInt(1, 4);
			}
		}
		if (fTile == null)
			fTile = fireTile();
		if (bTile == null)
			bTile = bankTile();
		GameObject Fire = Objects.getNearest(fire);
		switch (getState()) {
		case GETLOGS:
			if (Bank.isOpen()) {
				if (Bank.getCount(logs) > 0) {
					Bank.withdraw(logs, 1);
				} else {
					Methods.stopSub();
				}
			} else {
				if (bTile.isOnScreen()) {
					Bank.open();
				} else {
					Walking.findPath(bTile).traverse();
				}
			}
			break;
		case BANK:
			for (int i = 0; i <= 28; i++) {
				if (Inventory.getItemAt(i) != null) {
					if (Inventory.getItemAt(i).getId() != tinder
							&& Inventory.getItemAt(i).getId() != logs
							&& Inventory.getItemAt(i).getId() != raw
							&& Inventory.getItemAt(i).getId() != 995
							&& Inventory.getItemAt(i).getId() != cook
							&& Inventory.getItemAt(i).getId() != -1) {
						Inventory.getItemAt(i).interact("drop");
					}
				}
			}
			if (Bank.isOpen()) {

				Bank.depositAllExcept(tinder, logs);
				if (Bank.getCount(raw) > 0) {
					Bank.withdraw(raw, 26);
				} else {
					Methods.stopSub();
				}

			} else {
				if (bTile.isOnScreen()) {
					Bank.open();
				} else {
					Walking.findPath(bTile).traverse();
				}
			}
			break;
		case COOK:
			if (Fire.isOnScreen()) {
				if (Players.getLocal().getAnimation() == -1
						&& !animTimer.isRunning()) {
					if (Widgets.getComponent(905, 14).isValid()) {
						Widgets.getComponent(905, 14).click();
						animTimer.reset();
						sleep(1000);
					} else {
						Inventory.useItem(Inventory.getItem(raw), Fire);
						sleep(1000);
						if (cook == 0)
							setCooked();
					}
				} else if (Players.getLocal().getAnimation() != -1) {
					animTimer.reset();
					if (cook == 0)
						setCooked();
				} else {
					sleep(1000);
				}
			} else {
				Walking.findPath(Fire).traverse();
			}
			break;
		case LIGHT:
			if (Fire == null || !Fire.getLocation().equals(fTile)) {
				lightFire();
			}
			break;
		}

		if (Inventory.contains(raw)) {
			if (Bank.isOpen()) {
				Bank.close();
			}
		}
	}

	private void setCooked() {
		for (int j = 0; j < cooked.length; j++) {
			if (Inventory.contains(cooked[j])) {
				cook = cooked[j];
			}
		}
	}

	private void lightFire() {
		if (!Players.getLocal().getLocation().equals(fTile)) {
			if (fTile.isOnScreen()) {
				if (!Players.getLocal().isMoving()) {
					fTile.interact("");
					sleep(500);
				}
			} else {
				Walking.findPath(fTile).traverse();
			}
		} else {
			if (!animTimer.isRunning()) {
				Inventory.useItem(Inventory.getItem(tinder), Inventory
						.getItem(logs));
				animTimer.reset();
				sleep(1000);
			} else if (Players.getLocal().getAnimation() != -1) {
				animTimer.reset();
			}
		}
	}

	@Override
	public int[] required() {
		int[] req = { tinder };
		return req;
	}

	private Tile fireTile() {
		if (Methods.containsTile(Methods.GrandExchange, bankTile())) {
			if (ge == 1) {
				return new Tile(3148, 3483);
			} else if (ge == 2) {
				return new Tile(3148, 3500);
			} else if (ge == 3) {
				return new Tile(3180, 3500);
			} else if (ge == 4) {
				return new Tile(3180, 3483);
			}
		} else if (Methods.containsTile(Methods.VarrokWest, bankTile())) {
			return new Tile(3183, 3427);
		} else if (Methods.containsTile(Methods.VarrokEast, bankTile())) {
			return new Tile(3252, 3427);
		} else if (Methods.containsTile(Methods.FaladorEast, bankTile())) {
			return new Tile(3013, 3355);
		} else if (Methods.containsTile(Methods.Draynor, bankTile())) {
			return new Tile(3092, 3247);
		} else if (Methods.containsTile(Methods.Edgeville, bankTile())) {
			return new Tile(3089, 3490);
		}
		return null;
	}

	private Tile bankTile() {
		return Methods.getNearestBank().getCentralTile();
	}

	public void setInf(int raw, int log, String s) {
		this.raw = raw;
		this.logs = log;
		this.fishName = s;
	}

	@Override
	public boolean canSwitch() {
		return true;
	}

	@Override
	public String getStatus() {
		if (getState() == state.BANK) {
			return "Banking";
		} else if (getState() == state.COOK) {
			return "Cooking";
		} else {// making fire
			return "Making Fire";
		}
	}

	@Override
	public int skill() {
		return Skills.COOKING;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		if (e.getId() == MessageEvent.MESSAGE_ACTION) {
			if (e.getMessage().contains("You manage to cook")) {
				numFish++;
			}
		}
	}

	@Override
	public String[] paintString() {
		if (numFish > 0)
			return new String[] {
					"Time running: " + Timer.format(time.currTime()),
					"Fish cooked: " + numFish, "Cooked/Hour: " + cph(),
					"Profit/Hour: " + pph() };
		else
			return new String[] {
					"Time running: " + Timer.format(time.currTime()),
					"Fish cooked: 0", "Cooked/Hour: 0", "Profit/Hour: 0" };
	}

	private long pph() {
		// if (cook != 0) {
		// int cPrice = GeItem.lookup(cook).getGuidePrice();
		// int rPrice = GeItem.lookup(raw).getGuidePrice();
		// if (numFish > 0)
		// return getPerHour((cPrice * numFish) - (rPrice * numFish));
		// }
		return 0;
	}

	private long cph() {
		if (numFish > 0)
			return getPerHour(numFish);
		return 0;
	}

	private int getPerHour(final int value) {
		return (int) ((3600000.0 / (double) time.currTime()) * value);
	}

	@Override
	public Time getTime() {
		return time;
	}

}